package fi.jamk.ourcookingapp.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.json.JSONException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import fi.jamk.ourcookingapp.R;
import fi.jamk.ourcookingapp.backend.MyTools;
import fi.jamk.ourcookingapp.backend.architecture.manager.ReceiptManager;
import fi.jamk.ourcookingapp.backend.exceptions.EmptySearchException;
import fi.jamk.ourcookingapp.backend.exceptions.NoResultException;
import fi.jamk.ourcookingapp.backend.model.Ingredient;

/**
 * Activity for showing the filter options
 * @author Marius
 */
public class FilterActivity extends Activity {

	private final String NO_SELECT = "--";
	
	private List<Ingredient> lstIngredient;

	private Button btnFilter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filter);
		btnFilter = (Button) findViewById(R.id.btnFilter);
		try {
			initIngredientList();
		} catch (NoResultException | JSONException | InterruptedException | ExecutionException e) {
			Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * Initialize the list of ingredients to be shown
	 * @throws NoResultException
	 * @throws JSONException
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	private void initIngredientList() throws NoResultException, JSONException, InterruptedException, ExecutionException {
		lstIngredient = AppActivity.facade.getAllIngredients();
		List<String> ingsString = new ArrayList<String>();
		ingsString.add(NO_SELECT);
		for (Ingredient ing : lstIngredient) {
			ingsString.add(ing.getName());
		}
		initIngredientsSpinner(ingsString);
	}

	/**
	 * Initialize the spinner functionality
	 * @param ingsString
	 */
	private void initIngredientsSpinner(final List<String> ingsString) {
		
		int[] ids = {R.id.spnIng1, R.id.spnIng2, R.id.spnIng3};
		for (int i : ids) {
			final Spinner spinner = (Spinner) findViewById(i);
			ArrayAdapter<String> dataAdapater = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ingsString);
			dataAdapater.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner.setAdapter(dataAdapater);
		}
		// button for do the action
		btnFilter.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// retrieve filter data
				List<Ingredient> selected = new ArrayList<Ingredient>();
				int[] ids = {R.id.spnIng1, R.id.spnIng2, R.id.spnIng3};
				for (int i : ids) {
					Spinner spinner = (Spinner) findViewById(i);
					if(spinner.getSelectedItemPosition() > 0){
						selected.add(lstIngredient.get(spinner.getSelectedItemPosition()-1));
					}
				}
				List<String> selectedIDs = new ArrayList<String>();
				for (Ingredient ing : selected) {
					selectedIDs.add(ing.getID()+"");
				}
				
				EditText receiptName = (EditText) findViewById(R.id.receiptName);
				String strReceiptName = receiptName .getText().toString();
				EditText userName = (EditText) findViewById(R.id.userName);
				String strUserName = userName.getText().toString();
				// setting filter data
				try {
					if(!selectedIDs.isEmpty()){
						AppActivity.facade.setFilter(ReceiptManager.INGREDIENT, selectedIDs);
					}
					if(!MyTools.isEmpty(strReceiptName)){
						AppActivity.facade.setFilter(ReceiptManager.NAME, strReceiptName);
					}
					if(!MyTools.isEmpty(strUserName)){
						AppActivity.facade.setFilter(ReceiptManager.USER_LOGIN_NAME, strUserName);
					}
					// choose the stream tab
					AppActivity.tabHost.setCurrentTab(0);
				} catch (EmptySearchException e) {
				}
			}
		});
	}

	
}
